package com.library.gq.dao.imlp;
import com.library.gq.dao.DonateBookDao;
import com.library.gq.entity.BookEntity;
import com.library.gq.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class BookDaoImpl extends BaseDao implements DonateBookDao {
    String sql=null;

    /**
     * 查询全部遗忘物品
     * @return:count
     */
    @Override
    public Integer totalCount() {
         sql="SELECT count(1) FROM loss";

        ResultSet rs=executeQuery(sql);
        int count = 0;
        try {
            while (rs.next()){
               count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }

    /**
     * 按条件进行模糊查询
     * @param name:姓名
     * @param start:状态
     * @param pageSzie:页数
     * @return:list
     */
    @Override
    public List<BookEntity> list(String name, Integer start, Integer pageSzie) {
        List<BookEntity> list=new ArrayList<>();
        ResultSet rs=null;
        String sql=null;
        if (name.equals("全部")||name=="全部") {
            sql = "SELECT id,goodsName,time,state FROM loss where state=0 LIMIT ?,?;";
            rs=executeQuery(sql,start,pageSzie);
        }else if (!name.equals("全部")||name=="全部"){
            sql = "SELECT id,goodsName,time,state FROM loss where goodsName LIKE CONCAT('%',?,'%') and state=0 LIMIT ?,?";
            rs=executeQuery(sql,name,start,pageSzie);
        }
        try {
            while (rs.next()){
                Integer id=rs.getInt(1);
                String goodsName=rs.getString(2);
                String time=rs.getString(3);
                int states=rs.getInt(4);
                String state=null;
                if (states==0){
                    state="未领走";
                }else if(states==1){
                    state="已领走";
                }
                BookEntity bookEntity=new BookEntity(id,goodsName,time,state);
                list.add(bookEntity);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 新增物品
     * @param name:遗失的物品名
     * @param time:遗失时间
     * @param state:状态
     * @return count
     */
    @Override
    public Integer Insert(String name, String time, Integer state) {
         sql="INSERT INTO loss(goodsName,time,state)VALUES(?,?,?)";
        Integer count=executeUpdate(sql,name,time,state);
        return count;
    }

    /**
     * 根据id来进行修改
     * @param state:遗失物品的状态(0未领走，1已领走)
     * @param id:遗失物品编码(id)
     * @return
     */
    @Override
    public Integer update(Integer state,Integer id) {
         sql="UPDATE loss SET state=1 WHERE id=?;";
        Integer count=executeUpdate(sql,id);
        return count;
    }

    /**
     * 增加认领人的记录
     * @param goodsId :失物编码
     * @param name    :认领人姓名
     * @param card    :身份证号
     * @param time    :认领时间
     * @param phone   :联系方式
     * @return
     */
    @Override
    public Integer Insert_claim(Integer goodsId, String name,String card, String time, String phone) {
         sql="INSERT INTO claim(goodsId,`name`,Card,date,phone)VALUES(?,?,?,?,?)";
        Integer count=executeUpdate(sql,goodsId,name,card,time,phone);
        return count;
    }

    /**
     * 查询全部领取信息
     *
     * @return
     */
    @Override
    public Integer claimCount() {
         sql="SELECT count(1) FROM claim";
        ResultSet rs=executeQuery(sql);
        int count = 0;
        try {
            while (rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public List<BookEntity> list(Integer start, Integer pageSzie) {
        List<BookEntity> list=new ArrayList<>();
        ResultSet rs=null;
            sql = "SELECT id,goodsId,name,Card,date,phone FROM claim LIMIT ?,?;";
            rs=executeQuery(sql,start,pageSzie);
        try {
            while (rs.next()){
                Integer no=rs.getInt(1);
                Integer goodsId=rs.getInt(2);
                String name=rs.getString(3);
                String card=rs.getString(4);
               String date=rs.getString(5);
               String phone=rs.getString(6);
                BookEntity bookEntity=new BookEntity(no,goodsId,name,card,date,phone);
                list.add(bookEntity);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
